package org.kwin.tomcat;

import org.kwin.tomcat.annotation.WebServlet;
import org.kwin.tomcat.exception.ServletException;
import org.kwin.tomcat.netty.NettyServer;
import org.kwin.tomcat.servlet.http.HttpServlet;
import sun.net.NetworkServer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : wangkai
 * @version V1.0
 * @Package org.kwin.tomcat
 * @Description:
 * @date 2021年10月11日 18:24
 **/
public class Tomcat {
    
    private NettyServer nettyServer = new NettyServer();
    
    public void setPort(int port) {
        nettyServer.setPort(port);
    }
    
    public void addServlet(HttpServlet httpServlet) {
        if(httpServlet == null || !httpServlet.getClass().isAnnotationPresent(WebServlet.class)) {
            throw new ServletException("invalid httpservlet");
        }
        WebServlet webServlet = httpServlet.getClass().getAnnotation(WebServlet.class);
        String[] urlpstterns = webServlet.urlPatterns();
        for(String urlPattern : urlpstterns) {
            nettyServer.addServlet(urlPattern, httpServlet);
        }
    }
    
    public void start() throws Exception {
        nettyServer.start();
    }
    
}
